package com.nus.iss.ad.dbhelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.nus.iss.ad.model.DiscrepancyReport;
import com.nus.iss.ad.model.Employee;
import com.nus.iss.ad.model.Inventory;
import com.nus.iss.ad.model.Request;
import com.nus.iss.ad.model.RequestDetail;
import com.nus.iss.ad.model.SearchCriteria;
import com.nus.iss.ad.utils.JsonParser;
import com.nus.iss.ad.utils.MyConfig;

import android.R.string;
import android.widget.NumberPicker;

public class DiscrepancyDBHelper {
	
	
	
	public List<Inventory> getAllInventory(){
		List<Inventory>invList=new ArrayList<Inventory>();
		String url=MyConfig.BACKEND_PATH + "/api/getAllInventory";
		
		SearchCriteria searchCriteria=new SearchCriteria();

		try{
			JSONObject resultJSONObject=JsonParser.postStreamJSONObject(url, searchCriteria);
			JSONArray arr=resultJSONObject.getJSONArray("inventories");
			for(int i=0;i<arr.length();i++)
		     {
					JSONObject obj=arr.getJSONObject(i);
					Inventory inv=new Inventory();
					String id=obj.getString("inventoryId");
					inv.setInventoryId(id);
					invList.add(inv);
				}
		
		}catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return invList;
	}
	
	public Inventory getInventoryById(String id){
		String url=MyConfig.BACKEND_PATH + "/api/getInventoryById";
		url=url+"/"+id;
		Inventory inv=null;
		try{
		JSONObject jsonInventory=JsonParser.getJSONFromUrl(url);
		if(jsonInventory!=null){
		inv=new Inventory();
		inv.setInventoryId(jsonInventory.getString("inventoryId"));
		inv.setDescription(jsonInventory.getString("description"));
		inv.setStock(jsonInventory.getInt("stock"));
		}
	}catch
		(JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
	}
		return inv;
   }
	
	public void reportDiscrepancy(DiscrepancyReport dis) {

		String url=MyConfig.BACKEND_PATH + "/api/reportDiscrepancy";
		Gson gson=new Gson();
		String jsonDis=gson.toJson(dis);
		JsonParser.postStream(url,jsonDis);

	}
}
		 //using Gson
		 /*
		 String jElement=JsonParser.getStream(url);
		 Gson gson = new Gson();
       Inventory[] data = gson.fromJson(jElement, Inventory[].class);
       if(data==null){
       	data=new Inventory[0];
       }
       return data;
       
	}
	*/
